WPS Office.apk(点击下载) / CNOnlineFontManager.java


package cn.wps.moffice.fontmanager.internal;

import android.text.TextUtils;
import android.util.Log;
import cn.wps.moffice.OfficeApp;
import cn.wps.moffice.extlibs.Qing3rdLoginConstants;
import cn.wps.moffice_eng.R;
import com.appsflyer.share.Constants;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.iflytek.cloud.SpeechEvent;
import com.iflytek.cloud.SpeechUtility;
import defpackage.faf;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CNOnlineFontManager implements faf<fai> {
    private static final String csz = OfficeApp.arx().getString(R.string.wps_app_id);
    private fae fvM = new fae();
    private String fvN = OfficeApp.arx().arM().lZf;
    private String fvO;
    private File fvP = new File(this.fvN, ".wps-cn-online-fonts.db");
    private fah fvQ;
    private HashSet<String> fvR;

    /* access modifiers changed from: private */
    public int a(fai fai, boolean z, fno fno) {
        int a;
        if (fai == null || fai.fwt == null || ((!dlj.i(fai.fwt) && !dlj.k(fai.fwt)) || faf.a.fwk != (a = bsO()))) {
            if (!z || fno == null) {
                this.fvO = "";
            } else {
                this.fvO = OfficeApp.arx().arM().lZf + fno.userId + File.separator + fkc.a(z, fno);
                File file = new File(this.fvO);
                if (!file.exists()) {
                    file.mkdirs();
                }
            }
            a = this.fvM.a(this.fvO, fai);
            if (a != faf.a.fwj && a != faf.a.fwk && z && fno != null && fno.gcz != null) {
                String str = OfficeApp.arx().arM().lZf + fno.userId;
                long a2 = fkc.a(z, fno);
                File[] listFiles = new File(str).listFiles();
                if (listFiles != null) {
                    for (File file2 : listFiles) {
                        try {
                            int parseInt = Integer.parseInt(file2.getName());
                            if (((long) parseInt) < a2) {
                                a = this.fvM.a(str + File.separator + parseInt, fai);
                                if (a == faf.a.fwj || a == faf.a.fwk) {
                                    break;
                                }
                            } else {
                                continue;
                            }
                        } catch (NumberFormatException e) {
                        }
                    }
                }
            }
        }
        return a;
    }

    private fai a(JSONObject jSONObject, boolean z) {
        try {
            fai fai = new fai();
            fai.id = jSONObject.getString("id");
            if (jSONObject.has("name")) {
                fai.fwt = new String[]{jSONObject.getString("name")};
            }
            if (jSONObject.has("fontname")) {
                fai.fwt = new String[]{jSONObject.getString("fontname")};
            }
            fai.cvX = jSONObject.getInt("filesize");
            fai.size = fai.cvX;
            if (jSONObject.has("pic")) {
                fai.fws = jSONObject.getString("pic");
            }
            if (jSONObject.has("font_img")) {
                fai.fws = jSONObject.getString("font_img");
            }
            fai.fwu = new String[]{fai.id + ".ttf"};
            if (jSONObject.has(FirebaseAnalytics.Param.LEVEL)) {
                fai.fwr = jSONObject.getLong(FirebaseAnalytics.Param.LEVEL);
            }
            if (jSONObject.has(FirebaseAnalytics.Param.PRICE)) {
                fai.price = jSONObject.getInt(FirebaseAnalytics.Param.PRICE);
            }
            if (fai.fwt == null || !z) {
                return fai;
            }
            if (this.fvR == null) {
                this.fvR = new HashSet<>();
            }
            this.fvR.add(fai.fwt[0]);
            return fai;
        } catch (JSONException e) {
            e.toString();
            return null;
        }
    }

    static /* synthetic */ void a(CNOnlineFontManager cNOnlineFontManager, fak fak) {
        try {
            TreeMap treeMap = new TreeMap();
            treeMap.put("fontid", fak.id);
            treeMap.put("app", Qing3rdLoginConstants.WPS_UTYPE);
            HashMap hashMap = new HashMap();
            hashMap.put("cookie", "wps_sid=" + cqy.asP());
            JSONObject lA = lA(lwd.c("https://vip.wps.cn/font/download", lwd.r(treeMap), hashMap));
            if (lA != null && lA.has("url")) {
                fak.url = lA.getString("url");
                if (!TextUtils.isEmpty(fak.url)) {
                    fak.url = fak.url.toLowerCase();
                    if (!fak.url.startsWith("https://") && !fak.url.startsWith("http://")) {
                        fak.url = "https://" + fak.url;
                    }
                }
            }
        } catch (JSONException e) {
            e.toString();
        } catch (IOException e2) {
            e2.toString();
        }
    }

    private void bsP() {
        if (ebo.arU()) {
            fno bBh = fok.bBq().geJ.bBh();
            if (bBh != null) {
                this.fvO = OfficeApp.arx().arM().lZf + bBh.userId + File.separator + fkc.bxT();
                File file = new File(this.fvO);
                if (!file.exists()) {
                    file.mkdirs();
                    return;
                }
                return;
            }
            this.fvO = "";
            return;
        }
        this.fvO = "";
    }

    private List<fai> bsQ() throws IOException {
        if (this.fvQ == null) {
            if (!this.fvP.exists() || this.fvP.length() <= 0) {
                this.fvQ = new fah();
            } else {
                this.fvQ = (fah) lvb.readObject(this.fvP.getPath(), fah.class);
            }
        }
        if (this.fvQ == null) {
            this.fvQ = new fah();
        }
        if (this.fvQ.fonts == null) {
            this.fvQ.fonts = new ArrayList();
        }
        if (this.fvR == null) {
            this.fvR = new HashSet<>();
        }
        for (fai fai : this.fvQ.fonts) {
            if (!(fai == null || fai.fwt == null || fai.fwt.length <= 0)) {
                this.fvR.add(fai.fwt[0]);
            }
        }
        bsP();
        this.fvM.c(this.fvO, this.fvQ.fonts);
        return this.fvQ.fonts;
    }

    private static String h(List<String> list, String str) {
        if (list == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
            if (i != list.size() - 1) {
                sb.append(str);
            }
        }
        return sb.toString();
    }

    private static String j(Map<String, String> map) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            stringBuffer.append(entry.getKey()).append("=").append(entry.getValue());
        }
        stringBuffer.append("fxXAtde3jdKycGIUt5guobHIlkt1");
        return lvo.cm(stringBuffer.toString());
    }

    private static JSONObject lA(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            if ("download_font_ok".equals(jSONObject.get(SpeechUtility.TAG_RESOURCE_RESULT)) && jSONObject.has(SpeechEvent.KEY_EVENT_RECORD_DATA)) {
                return jSONObject.getJSONObject(SpeechEvent.KEY_EVENT_RECORD_DATA);
            }
        } catch (JSONException e) {
            e.toString();
        }
        return null;
    }

    private static JSONArray lB(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            if ("ok".equals(jSONObject.get(SpeechUtility.TAG_RESOURCE_RESULT)) && jSONObject.has(SpeechEvent.KEY_EVENT_RECORD_DATA)) {
                JSONObject jSONObject2 = jSONObject.getJSONObject(SpeechEvent.KEY_EVENT_RECORD_DATA);
                if (jSONObject2.has(SpeechEvent.KEY_EVENT_RECORD_DATA)) {
                    return jSONObject2.getJSONArray(SpeechEvent.KEY_EVENT_RECORD_DATA);
                }
            }
        } catch (JSONException e) {
            e.toString();
        }
        return null;
    }

    private List<fai> m(int i, int i2, boolean z) {
        fah fah;
        try {
            TreeMap treeMap = new TreeMap();
            OfficeApp arx = OfficeApp.arx();
            treeMap.put("page", "1");
            treeMap.put("limit", "100");
            treeMap.put("position", "android_rec_font");
            treeMap.put("app_id", csz);
            treeMap.put("v", arx.getString(R.string.app_version));
            treeMap.put(Constants.URL_CAMPAIGN, arx.arA());
            treeMap.put("pc", arx.arB());
            treeMap.put("p", arx.getPackageName());
            treeMap.put("sig", j(treeMap));
            JSONArray lB = lB(lwd.c("https://vip.wps.cn/font/api/client/recommend", lwd.r(treeMap), null));
            if (lB == null) {
                return null;
            }
            ArrayList arrayList = new ArrayList();
            for (int i3 = 0; i3 < lB.length(); i3++) {
                fai a = a(lB.getJSONObject(i3), true);
                if (a != null) {
                    arrayList.add(a);
                }
            }
            new StringBuilder("server return fonts: ").append(arrayList.size());
            this.fvQ.fonts = arrayList;
            this.fvQ.fwo = System.currentTimeMillis();
            if (!(this.fvP == null || !this.fvP.exists() || (fah = (fah) lvb.readObject(this.fvP.getPath(), fah.class)) == null)) {
                this.fvQ.fwp = fah.fwp;
            }
            lvb.writeObject(this.fvQ, this.fvP.getPath());
            return arrayList;
        } catch (IOException e) {
            e.toString();
            return null;
        } catch (JSONException e2) {
            e2.toString();
            return null;
        }
    }

    /* access modifiers changed from: private */
    /* renamed from: pt */
    public fai py(String str) {
        try {
            JSONArray pw = pw(lwd.f("https://vip.wps.cn/font/api/client/fontsinfo?ids=" + str, null));
            if (pw == null || pw.length() != 1) {
                return null;
            }
            return a(pw.getJSONObject(0), false);
        } catch (IOException e) {
            e.toString();
            return null;
        } catch (JSONException e2) {
            e2.toString();
            return null;
        }
    }

    /* access modifiers changed from: private */
    /* renamed from: pv */
    public fai px(String str) {
        if (this.fvQ == null || this.fvQ.fonts == null) {
            try {
                bsQ();
            } catch (IOException e) {
                Log.e("OnlineFont", e.toString());
            }
        }
        if (!(this.fvQ == null || this.fvQ.fonts == null)) {
            for (fai fai : this.fvQ.fonts) {
                if (fai.fwt != null && fai.fwt.length > 0 && fai.fwt[0].equals(str)) {
                    return fai;
                }
            }
        }
        return null;
    }

    private static JSONArray pw(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            if ("ok".equals(jSONObject.get(SpeechUtility.TAG_RESOURCE_RESULT)) && jSONObject.has(SpeechEvent.KEY_EVENT_RECORD_DATA)) {
                return jSONObject.optJSONArray(SpeechEvent.KEY_EVENT_RECORD_DATA);
            }
        } catch (JSONException e) {
            e.toString();
        }
        return null;
    }

    @Override // defpackage.faf
    public final long L(long j) {
        return fae.L(j);
    }

    @Override // defpackage.faf
    public final List<fai> av(List<String> list) {
        TreeMap treeMap = new TreeMap();
        treeMap.put("display", h(list, "|"));
        try {
            JSONArray pw = pw(lwd.c("https://vipapi.wps.cn/font/docfontlist", lwd.r(treeMap), null));
            if (pw == null) {
                return null;
            }
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < pw.length(); i++) {
                fai a = a(pw.getJSONObject(i), false);
                if (a != null) {
                    arrayList.add(a);
                }
            }
            return arrayList;
        } catch (IOException e) {
            e.toString();
            return null;
        } catch (JSONException e2) {
            e2.toString();
            return null;
        }
    }

    @Override // defpackage.faf
    public final boolean bsM() {
        fah fah;
        if (this.fvQ == null) {
            try {
                bsQ();
            } catch (IOException e) {
                e.toString();
            }
            return this.fvQ.fwp;
        } else if (this.fvQ.fwp || !this.fvP.exists() || (fah = (fah) lvb.readObject(this.fvP.getPath(), fah.class)) == null) {
            return true;
        } else {
            return fah.fwp;
        }
    }

    @Override // defpackage.faf
    public final boolean bsN() {
        fah fah;
        if (this.fvQ == null) {
            try {
                bsQ();
            } catch (IOException e) {
                e.toString();
            }
            return this.fvQ.fwq;
        } else if (this.fvQ.fwq || !this.fvP.exists() || (fah = (fah) lvb.readObject(this.fvP.getPath(), fah.class)) == null) {
            return true;
        } else {
            return fah.fwq;
        }
    }

    @Override // defpackage.faf
    public final int bsO() {
        return fag.bsV().bsO();
    }

    /* JADX DEBUG: Method arguments types fixed to match base method, original types: [fak] */
    @Override // defpackage.faf
    public final /* synthetic */ void f(fai fai) {
        String[] strArr = fai.fwu;
        if (strArr != null) {
            for (String str : strArr) {
                new File(this.fvO, str);
                fae.bsR();
            }
        }
    }

    /* JADX DEBUG: Method arguments types fixed to match base method, original types: [fak] */
    @Override // defpackage.faf
    public final /* synthetic */ int g(fai fai) {
        fai fai2 = fai;
        boolean arU = ebo.arU();
        return a(fai2, arU, arU ? fok.bBq().geJ.bBh() : null);
    }

    /* JADX DEBUG: Method arguments types fixed to match base method, original types: [fak] */
    /* JADX INFO: finally extract failed */
    @Override // defpackage.faf
    public final /* synthetic */ void h(fai fai) throws IOException {
        boolean z;
        final fai fai2 = fai;
        if (fai2 instanceof fai) {
            z = ebo.arU() && fkc.bxT() >= ((long) ((int) fai2.fwr));
        } else {
            z = false;
        }
        if (z && !fai2.fwv && !fai2.cvj) {
            bsP();
            File file = new File(this.fvN, fai2.id + ".tmp");
            new StringBuilder("lock file: ").append(file.toString());
            if (!file.exists() || System.currentTimeMillis() - file.lastModified() >= 30000) {
                fai2.fwv = true;
                try {
                    fae.a(this.fvO, this.fvN, fai2, new Runnable() {
                        /* class cn.wps.moffice.fontmanager.internal.CNOnlineFontManager.AnonymousClass1 */

                        public final void run() {
                            CNOnlineFontManager.a(CNOnlineFontManager.this, fai2);
                        }
                    });
                    fai2.fwv = false;
                } catch (IOException e) {
                    e.toString();
                    if (file.exists()) {
                        file.delete();
                    }
                    fai2.fwv = false;
                    fae.a((fak) fai2, false, false, false);
                    fai2.fwv = false;
                } catch (Throwable th) {
                    fai2.fwv = false;
                    throw th;
                }
            }
        }
    }

    @Override // defpackage.faf
    public final List<fai> kh(boolean z) throws IOException {
        if (this.fvQ == null || this.fvQ.fonts == null || this.fvQ.fonts.size() <= 0 || Math.abs(System.currentTimeMillis() - this.fvQ.fwo) >= 14400000) {
            bsQ();
            return !z ? this.fvQ.fonts : m(1, 100, true);
        }
        bsP();
        this.fvM.c(this.fvO, this.fvQ.fonts);
        return this.fvQ.fonts;
    }

    @Override // defpackage.faf
    public final void ki(boolean z) {
        if (this.fvQ == null) {
            try {
                bsQ();
            } catch (IOException e) {
                e.toString();
            }
        }
        this.fvQ.fwp = z;
        lvb.writeObject(this.fvQ, this.fvP.getPath());
    }

    @Override // defpackage.faf
    public final void kj(boolean z) {
        fah fah;
        if (this.fvQ == null) {
            try {
                bsQ();
            } catch (IOException e) {
                e.toString();
            }
        }
        this.fvQ.fwq = true;
        if (!(this.fvP == null || !this.fvP.exists() || (fah = (fah) lvb.readObject(this.fvP.getPath(), fah.class)) == null)) {
            this.fvQ.fwp = fah.fwp;
        }
        lvb.writeObject(this.fvQ, this.fvP.getPath());
    }

    @Override // defpackage.faf
    public final String ps(String str) {
        TreeMap treeMap = new TreeMap();
        treeMap.put("display", str);
        try {
            JSONArray pw = pw(lwd.c("https://vipapi.wps.cn/font/docfontlist", lwd.r(treeMap), null));
            if (pw == null || pw.length() != 1) {
                return null;
            }
            JSONObject jSONObject = pw.getJSONObject(0);
            if (!jSONObject.has(FirebaseAnalytics.Param.PRICE) || jSONObject.getInt(FirebaseAnalytics.Param.PRICE) <= 0) {
                return jSONObject.getString("id");
            }
            return null;
        } catch (IOException e) {
            e.toString();
            return null;
        } catch (JSONException e2) {
            e2.toString();
            return null;
        }
    }

    @Override // defpackage.faf
    public final boolean pu(String str) {
        if (this.fvR == null) {
            try {
                bsQ();
            } catch (IOException e) {
                Log.e("OnlineFont", e.toString());
            }
        }
        return this.fvR != null && this.fvR.contains(str);
    }
}